console.log("...init");
console.log(window.dk_mode);

//--------------------- 普通模式

if (!window.dk_mode) {
  var __text = "";
  setInterval(() => {
    if (document.body) {
      document.body.addEventListener(
        "mouseup",
        () => {
          let text = document.getSelection().toString();
          text = replaceSpace(text);
          if (text !== __text) {
            __text = text;
            searchDkCourse(text);
          }
        },
        false
      );
    }
  }, 100);
}

//---------------------------------------- iframe模式模式

if (window.dk_mode === "iframe") {
  document.body.innerHTML = `
  <iframe id='myIframe' src="${window.location.href}"
  style="width: 100%;height: 100%;overflow: hidden;">
  </iframe>
  `;
  document.body.style.overflow = "hidden";

  var __text = "";
  setInterval(() => {
    const ifDoc = document.getElementById("myIframe").contentDocument;
    if (ifDoc?.body && !ifDoc.body.myInit) {
      ifDoc.body.myInit = true;
      ifDoc.body.addEventListener(
        "mouseup",
        () => {
          let text = ifDoc.getSelection().toString();
          text = replaceSpace(text);
          if (text !== __text) {
            __text = text;
            onDkGgSelectText(text);
          }
        },
        false
      );
    }
  }, 100);
}

function onDkGgSelectText(text) {
  searchDkCourse(text);
}

//----------------------

var currJsElInfo = (() => {
  //   document.currentScript.src  获取的是包含绝对路径
  var currSrc = document.currentScript.getAttribute("src");
  return {
    src: currSrc,
    srcDir: currSrc.substring(0, currSrc.lastIndexOf("/") + 1),
  };
})();

console.log(currJsElInfo);

//---------------------

let iframeUrl = currJsElInfo.srcDir + "course.html";

const dk_pan = document.createElement("div");
dk_pan.innerHTML = `
  <div id="dk-course-iframe-pan">
  <iframe
    src="${iframeUrl}"
    frameborder="0"
    id="dk-course-iframe"
    onload="dk_course_iframe_load()"
  ></iframe>
  <style>
    #dk-course-iframe-pan{
      position: fixed;
      left: 0;
      top: 0;
      width: 100%;
    }
    #dk-course-iframe {
      background-color: #fff;
      z-index: 999;
      position: fixed;
      right: 0;
      top: 50px;
      border: 5px solid #ddd;
      width: 630px;
      height: 700px;
      border-top: 20px solid #aaa;
    }
  </style>
</div> 
  `;
document.body.append(dk_pan);

//--------------------

function dk_course_iframe_load() {
  console.log("load course.html iframe end ....");

  var pan = document.getElementById("dk-course-iframe-pan");
  var iframe = document.getElementById("dk-course-iframe");
  console.log("iframe ", iframe);
  if (!iframe) return;

  var isDragging = false; // 用于判断是否正在拖拽
  var top = 50;
  var right = 0;
  var offsetX = 0;
  var offsetY = 0;

  iframe.addEventListener("mousedown", function (e) {
    console.log("开始移动");
    isDragging = true; // 标记开始拖拽
    pan.style.height = "100%";
    offsetX = e.clientX;
    offsetY = e.clientY;
  });
  document.addEventListener("mousemove", function (e) {
    if (isDragging) {
      right = right - (e.clientX - offsetX);
      top = top + (e.clientY - offsetY);
      offsetX = e.clientX;
      offsetY = e.clientY;
      iframe.style.right = right + "px";
      iframe.style.top = top + "px";
    }
  });
  document.addEventListener("mouseup", function (e) {
    console.log("结束移动");
    console.log(right, top);
    isDragging = false; // 标记结束拖拽
    pan.style.height = "0";
  });
}

//---------------------

function searchDkCourse(searchValue) {
  var iframe = document.getElementById("dk-course-iframe");
  if (!iframe) return;
  // 向iframe发送消息
  iframe.contentWindow.postMessage(
    {
      dk_course: true,
      searchValue,
    },
    "*"
  );
}

//--------------------

function replaceSpace(str) {
  if (!str) return str;
  str = str.replace(/\n/g, "@-n");
  str = str.replace(/\s/g, " ");
  str = str.replace(/@-n/g, "\n");
  return str;
}
